cc.Class({
    extends: cc.Component,

    properties: {
        model:sp.Skeleton,
        ani_name:'',     
        skin:'default',   
        listen_com:false,  // 是否开启完结监听
    },
    // 加载动画
    initInfo(param,skin,force){
        if(force){
            he.load.getRes('spine/buff/'+param,sp.SkeletonData,this.loadOver.bind(this))
        }else{
            he.load.getRes(param,sp.SkeletonData,this.loadOver.bind(this))
        }
        if(skin){
            this.skin = skin
        }
        let ani_list = param.split("/")
        this.ani_name = ani_list[ani_list.length - 1]
    },
    // 加载播放
    loadOver(err,asset){
        if(err){
            he.error(err,"buff动画加载错了")
            return
        }
        this.model.skeletonData = asset

        // 内容调整
        switch(this.ani_name){
            default:
                this.onSpineListen()
            break
        }
        this.play()
    },
    clearInfo(){
        this.node.position = cc.Vec2.ZERO
        if(this.listen_com){
            this.listen_com = false
            this.model.setCompleteListener(null)
        }
        this.model.skeletonData = null
        this.skin = 'default'
        he.pool.putElement("buffAni",this.node)
    },
    onSpineListen(){
        this.listen_com = true
        this.model.setCompleteListener((trackEntry) => {
            var animationName = trackEntry.animation ? trackEntry.animation.name : "";
            this.listenAniTag(animationName)
        });
    },
    listenAniTag(ani){
        switch(ani){
            default:
                this.node.active = false
            break
        }
    },
    play(){
        if(!this.ani_name){
          return
        }
        if(!this.model || !this.model.skeletonData){
            return
        }
        this.node.active = true

        switch(this.skin){
            default:
                this.model.defaultSkin = this.skin
                this.model.setSkin(this.skin)
            break
        }
        switch(this.ani_name) {
            default:
                this.model.defaultAnimation = `hit`
                this.model.setAnimation(0,'hit',true)
            break
        }
    },
});
